package com.demo.servlet.utils;


import com.aspose.words.*;
import com.demo.servlet.entity.Staff;
import javafx.scene.chart.PieChart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class CreateChartTest {

    public static final String DocumentBuilderAndSaveOut = "DocumentBuilderAndSaveOut.docx";
    public static final String output = "./output/";
    public static final String school_template = "school_template.docx";
    public static final long datast = new Date().getTime();
    private static final Logger LOGGER = LoggerFactory.getLogger(PieChart.class);

    public static void main(String[] args) throws  Exception {
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        long old = System.currentTimeMillis();
        List<Staff> entity1 = new ArrayList<>();
        double[] double_one = new double[]{100,200};
        //double[] double_two = new double[]{150};

        Staff a = new Staff("信息学院", double_one);
        //Staff b = new Staff("第二个学院", double_two);
        entity1.add(a); //entity1.add(b);
        CreateChart(doc,builder,"标题",entity1);

        /**********************************************************/
        double[] doubles = new double[]{100,200,500,300,800,10};


        /********************************************************************/
        save(doc,old);
    }



    /*柱状图*/
    public static void CreateChart(Document doc, DocumentBuilder builder, String Title,List<Staff> entity) throws Exception {
        // 添加带有默认数据的图表。您可以指定不同的图表类型和大小
        Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252);
        // 形状的图表属性包含所有图表相关选项
        Chart chart = shape.getChart();
        chart.getTitle().setText(Title);//标题
        // 获取图表系列收集
        ChartSeriesCollection seriesColl = chart.getSeries();
        // 删除默认生成的系列.
        seriesColl.clear();
        // 创建类别名称数组，在本例中我们有两个类别.
        String[] categories = new String[]{"柱状图对比"};
        // Adding new series. Please note, data arrays must not be empty and arrays must be the same size.
        entity.forEach(c->{
            seriesColl.add(c.getName(), categories,c.getDoubles());
        });
        Iterator<ChartSeries> iterator = seriesColl.iterator();
        while (iterator.hasNext()) {
            ChartSeries series = iterator.next();
            ChartDataLabelCollection dataLabelCollection = series.getDataLabels();
            dataLabelCollection.add(0);
            Iterator<ChartDataLabel> iterator1 = dataLabelCollection.iterator();
            while (iterator1.hasNext()) {
                ChartDataLabel chartDataLabel = iterator1.next();
                chartDataLabel.setShowValue(true);
            }
        }
    }

    /*冒泡排序*/


    private  static  void  save(Document doc,long old)throws Exception{
        String docxName = datast+DocumentBuilderAndSaveOut;
        doc.save(output+docxName);
        long now = System.currentTimeMillis();
        System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒"); // 转化用时
        doc.save(output + datast + DocumentBuilderAndSaveOut);
    }


}